package cn.hn.comm.mail;

//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class SimpleMailSender {

	public static String getCode(int i)
	{
		String strCode=i+"";
		if(strCode.length()<2)
		{
			strCode="0"+strCode;
		}
		return strCode;
	}	
	
	public static void main(String[] args) {
		
//		for(int i=2;i<25;i++)
//		{
//			MailSendResult result = SimpleMailSender.SendHtmlMail("smtp.qq.com", "tianxin_mail"+SimpleMailSender.getCode(i),
//					"tongyongMM", "602835713@qq.com", "tianxin_mail"+SimpleMailSender.getCode(i)+"@qq.com",
//					System.currentTimeMillis()+"", System.currentTimeMillis()+"");
//			System.out.println("tianxin_mail"+SimpleMailSender.getCode(i)+"结果--------"+result.getInfo());
//		}
		
//		MailSendResult result = SimpleMailSender.SendHtmlMail("smtp.qq.com", "tianxin_mail08",
//				"tongyongMM", "yuannana2006@163.com", "tianxin_mail08@qq.com",
//				"javamail test测试2", "test 测试2");
//		System.out.println(result.getInfo());
		// Mail mail = new Mail();
		// mail.sendMsg("602835713", "19840302d", "stmp.qq.com",
		// "119shenhao@163.com", "602835713@qq.com", "javamail test测试",
		// "test 测试");
		
		
		MailSendResult result = SimpleMailSender.SendHtmlMail("tianxin007.gnway.net", "sh",
				"tongyongMM", "yuannana2006@126.com", "sh@tianxin.com",
				"不是的", "对方的水电费的发生地法萨芬");
		System.out.println(result.getInfo());		
		
//		MailSendResult result = SimpleMailSender.SendMail("192.168.1.102", "sh",
//				"123456", "602835713@qq.com", "sh@192.168.1.102",
//				"javamail test", "test");
//		System.out.println(result.getInfo());		
		
	}
	
	public static MailSendResult SendHtmlMail(String host, String userName,
			String password, String toAddress, String fromAddress,
			String subject, String text) {
		MailSendResult msr=new MailSendResult();
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			// 设定mail server
			sender.setHost(host);
			sender.setUsername(userName);
			sender.setPassword(password);


	        MimeMessage mailMessage = sender.createMimeMessage();   
	        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"UTF-8");   
	                       
	        //设置收件人，寄件人   
	        messageHelper.setTo(toAddress);   
	        messageHelper.setFrom(fromAddress);   
	        messageHelper.setSubject(subject);   
	        //true 表示启动HTML格式的邮件   
	        messageHelper.setText(text,true);   
	           
	        //发送邮件   
	        sender.send(mailMessage);  
			msr.setState(MailSendResult.STATE_SUC);
			msr.setInfo("邮件发送成功");
		} catch (Exception e) {
			msr.setState(MailSendResult.STATE_FAIL);
			msr.setInfo(e.toString());
		}
		return msr;
	}	
	
	
	public static MailSendResult SendHtmlMail_SSL(String host, String userName,
			String password, String toAddress, String fromAddress,
			String subject, String text) {
		MailSendResult msr=new MailSendResult();
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			// 设定mail server
			sender.setHost(host);
			sender.setUsername(userName);
			sender.setPassword(password);
			Properties config = new Properties();  
	        config.put("mail.smtp.auth", "true");
	        config.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	        config.put("mail.smtp.socketFactory.fallback", "false");
	        config.put("mail.smtp.port", "465");
	        config.put("mail.smtp.socketFactory.port", "465");
	        ((JavaMailSenderImpl)sender).setJavaMailProperties(config);  


	        MimeMessage mailMessage = sender.createMimeMessage();   
	        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"UTF-8");   
	                       
	        //设置收件人，寄件人   
	        messageHelper.setTo(toAddress);   
	        messageHelper.setFrom(fromAddress);   
	        messageHelper.setSubject(subject);   
	        //true 表示启动HTML格式的邮件   
	        messageHelper.setText(text,true);   
	           
	        //发送邮件   
	        sender.send(mailMessage);  
			msr.setState(MailSendResult.STATE_SUC);
			msr.setInfo("邮件发送成功");
		} catch (Exception e) {
			msr.setState(MailSendResult.STATE_FAIL);
			msr.setInfo(e.toString());
		}
		return msr;
	}
	

	public static MailSendResult SendMail(String host, String userName,
			String password, String toAddress, String fromAddress,
			String subject, String text) {
		MailSendResult msr=new MailSendResult();
		try {
			JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
			// 设定mail server
			senderImpl.setHost(host);
			senderImpl.setUsername(userName);
			senderImpl.setPassword(password);

			// 建立邮件消息
			SimpleMailMessage mailMessage = new SimpleMailMessage();
			// 设置收件人，寄件人
			// String[] array = new String[]
			// {"sun111@163.com","sun222@sohu.com"};
			// mailMessage.setTo(array);
			mailMessage.setTo(toAddress);
			mailMessage.setFrom(fromAddress);
			mailMessage.setSubject(subject);
			mailMessage.setText(text);

			// 发送邮件
			senderImpl.send(mailMessage);
			
			msr.setState(MailSendResult.STATE_SUC);
			msr.setInfo("邮件发送成功");
		} catch (Exception e) {
			msr.setState(MailSendResult.STATE_FAIL);
			msr.setInfo(e.toString());
		}
		return msr;
	}

	public static String SendMails(String host, String userName,
			String password, String[] toAddresses, String fromAddress,
			String subject, String text) {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		// 设定mail server
		senderImpl.setHost(host);
		senderImpl.setUsername(userName);
		senderImpl.setPassword(password);

		// 建立邮件消息
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		// 设置收件人，寄件人
		// String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
		mailMessage.setTo(toAddresses);
		mailMessage.setFrom(fromAddress);
		mailMessage.setSubject(subject);
		mailMessage.setText(text);

		// 发送邮件
		senderImpl.send(mailMessage);

		return "邮件发送成功";
	}
}
